package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsRoleMapper;
import com.qf.pojo.DtsRole;
import com.qf.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private DtsRoleMapper dtsRoleMapper;

    @Override
    public Set<String> findRolesByRoleIds(Integer[] roleIds) {
        LambdaQueryWrapper<DtsRole> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.select(DtsRole::getName)
                .in(DtsRole::getId,roleIds)
                .eq(DtsRole::getEnabled,true)
                .eq(DtsRole::getDeleted,false);
        List<DtsRole> dtsRoleList=dtsRoleMapper.selectList(queryWrapper);

        return dtsRoleList.stream().map(DtsRole::getName).collect(Collectors.toSet());
    }

    @Override
    public List<DtsRole> findAllRoles() {
        return dtsRoleMapper.selectList(null);
    }

    @Override
    public IPage<DtsRole> findRolePageByQuery(String rolename, String sort, String order, Integer page, Integer limit) {
        return dtsRoleMapper.selectPage(new Page<DtsRole>(page,limit)
                ,new QueryWrapper<DtsRole>()
                        .like(!StringUtils.isEmpty(rolename),"name",rolename)
                        .eq("deleted",false)
                        .orderBy(!StringUtils.isEmpty(sort),"ASC".equalsIgnoreCase(order),sort)
        );
    }

    @Override
    public void insertRole(DtsRole dtsRole) {
        dtsRoleMapper.insert(dtsRole);
    }

    @Override
    public void deleteRole(DtsRole dtsRole) {
        dtsRoleMapper.updateById(dtsRole);
    }

    @Override
    public void updateRole(DtsRole dtsRole) {
        dtsRoleMapper.updateById(dtsRole);
    }
}
